$(function() {
  Date.prototype.Format = function(fmt) {
    //author: 
    var o = {
      "M+": this.getMonth() + 1, //月份
      "d+": this.getDate(), //日
      "h+": this.getHours(), //小时
      "m+": this.getMinutes(), //分
      "s+": this.getSeconds(), //秒
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度
      S: this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1
            ? o[k]
            : ("00" + o[k]).substr(("" + o[k]).length)
        );
    return fmt;
  };

  window.getUrlParam = function(name) {
    var reg = new RegExp("(\\?|#|&)" + name + "=([^&#]*)(&|#|$)");
    var result = window.location.hash.match(reg);
    return result ? decodeURIComponent(result[2]) : null;
  };

  /**
   * 使用  $("#searchForm").serializeObject();
   * @desc {{description}}{{form表单序列化为json对象}}
   * 
   */
  $.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
      if (o[this.name]) {
        if (!o[this.name].push) {
          o[this.name] = [o[this.name]];
        }
        o[this.name].push(this.value || "");
      } else {
        o[this.name] = this.value || "";
      }
    });
    return o;
  };

  /**
   * @desc {{description}}{{退出函数}}
   * 
   */
  function logout() {
    newTime = new Date().getTime();
    $.ajax({
      url: "api/logout?time=" + newTime,
      type: "post",
      cache: false,
      dataType: "json",
      contentType: "application/json",
      success: function(data) {
        // $.cookie("token", null);
        sessionStorage.removeItem("token");
        window.location.href = "login.html";
      },
      error: function() {
        // $.cookie("token", null);
        sessionStorage.removeItem("token");
        window.location.href = "login.html";
      }
    });
    // $.cookie("token", null);
    sessionStorage.removeItem("token");
    window.location.href = "login.html";
  }

  /**
   * 
   * @desc {{description}}{{修改密码功能函数}}
   */
  function changePass() {
    $.ajax({
      type: "GET",
      url: "module/changePassTpl.html",
      success: function(data) {
        g_dialog.dialog($(data).find("[id=changePassTpl]"), {
          width: "560px",
          init: init,
          initAfter: initAfter,
          title: "修改密码",
          saveclick: save_click
          // autoHeight: "autoHeight"
        });
      }
    });

    function init(el) {}
    Array.prototype.baoremove = function(dx) {
      if (isNaN(dx) || dx > this.length) {
        return false;
      }
      this.splice(dx, 1);
    };

    function initAfter(el) {
      //  初始化之后的操作
    }

    function save_click(el, saveObj) {
      if (validateForm($("#changePassForm"))) {
        xd_ajax_post({
          url: "api/resetPassword",
          paramObj: saveObj,
          // maskObj: el,
          successCallBack: function(data) {
            g_dialog.operateAlert("修改密码成功，请重新登录");
            g_dialog.hide(el);
            alert("修改密码成功，请重新登录");
            sessionStorage.removeItem("token");
            window.location.href = "login.html";
          }
        });
      }
    }
  }

  var flag = true
  $(document).ajaxError(function(event, request, settings) {
    // layer.closeAll('loading')
    if (request.status == 401) {
        if (flag) {
            alert("登录超时，请重新登录");
            window.location.href = "login.html";
            // $.cookie("token", null);
            sessionStorage.removeItem("token");
            flag = !flag
        }

    }
    if (request.status == 404) {
        alert("请求失败");
        // $.cookie("token", null)
        // window.location.href = "login.html"

    }
    if (request.status == 500) {
        alert("服务器异常");

    }
    if (request.status == 504) {
        alert("请求超时,请重试");
        // $.cookie("token", null)
        // window.location.href = "login.html"
    }
});

  var token = sessionStorage.getItem("token")
  if(!token){
    window.location.href = "login.html";
  }
  $("#logout").click(function() {
    logout();
  });

  $("#changePass").click(function() {
    changePass();
  });

  /**
 * 
 * @des   清除表单验证样式
 * @param {any} form jquery对象$("#formId")
 */
 window.clearValidatorStyle=function (form) {
  var inputs = form.find('input')
  var selects = form.find('select')
  inputs.removeClass('input_validation-failed');
  selects.removeClass('input_validation-failed');
  inputs.removeAttr('tip');
  selects.removeAttr('tip');

}
});
